<template>
	<el-container>
		<div v-show="screenVisible" class="table-screen-column">
			<el-form label-width="auto" label-position="right">
				<el-row :gutter="20">
				  <el-col :span="4" >
				  	<el-form-item label="分组">
						<el-select v-model="search.group" clearable placeholder="分组">
  						  	<el-option label="服务端" value="u"></el-option>
  						  	<el-option label="客户端" value="m"></el-option>
  						</el-select>
					</el-form-item>
				  </el-col>
				  <el-col :span="4">
                    <el-form-item label="ip地址">
						<el-input v-model="search.ip" placeholder="ip地址" clearable></el-input>
					</el-form-item>
				  </el-col>
				</el-row>
			</el-form>
		</div>
		<el-header>
			<div class="left-panel">
                <div class="right-panel-search">
                    <el-button-group>
                        <el-button :type="search.type == 'image' ? 'primary' : ''" @click="handleType('image')">图片</el-button>
                        <el-button :type="search.type == 'video' ? 'primary' : ''" @click="handleType('video')">视频</el-button>
                        <el-button :type="search.type == 'audio' ? 'primary' : ''" @click="handleType('audio')">音频</el-button>
                        <el-button :type="search.type == 'other' ? 'primary' : ''" @click="handleType('other')">其他文件</el-button>
                    </el-button-group>
                    <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
                </div>
            </div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.name" placeholder="文件名称" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" plain @click="handleQuery">搜索</el-button>
					<el-button @click="resetQuery" icon="el-icon-refresh">重置</el-button>
					<el-button type="primary" link @click="screenVisible = !screenVisible">
            		  {{ screenVisible ? '关闭更多筛选' : '显示更多筛选'}}
            		  <el-icon><el-icon-arrow-down v-if="screenVisible" /><el-icon-arrow-up v-else /></el-icon>
            		</el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<el-table ref="table" v-loading="loading" :data="tableList" @selection-change="selectionChange" border>
                <el-table-column type="selection" width="55" :selectable="handleChechBox"></el-table-column>
				<el-table-column label="#" prop="id" width="100" align="center"></el-table-column>
				<el-table-column label="文件" prop="name">
					<template #default="scope">
                        <el-image v-if="scope.row.type == 'image'" style="height: 60px; width: auto" :src="scope.row.path" :fit="scope.row.name"/>
						<span v-else>{{ scope.row.name }}</span>
                    </template>
				</el-table-column>
				<el-table-column label="分组" prop="group" width="120">
                    <template #default="scope">
                        <el-tag v-if="scope.row.group=='m'" type="success">服务端</el-tag>
						<el-tag v-else type="info">客户端</el-tag>
                    </template>
                </el-table-column>
				<el-table-column label="文件大小" prop="size" width="120"></el-table-column>
				<el-table-column label="路径" prop="path" width="400"></el-table-column>
				<el-table-column label="ip地址" prop="ip" width="160"></el-table-column>
				<el-table-column label="备注" prop="notes"></el-table-column>
				<el-table-column label="上传时间" prop="created_at" width="160"></el-table-column>
                <el-table-column label="操作" fixed="right" align="center" width="170">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
						</el-button-group>
					</template>
				</el-table-column>
			</el-table>
		</el-main>
		<el-footer>
			<sc-page v-model="search"></sc-page>
		</el-footer>
	</el-container>

    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
    import saveDialog from './save'

	export default {
		name: 'resource',
		components: {
            saveDialog
        },
		data() {
			return {
                dialog: {
					save: false,
				},
				loading: true,
				screenVisible: false,
				tableList: [],
				selection: [],
				search: {
					page: 1,
        			pageSize: 20,
					total: 0,
					paging: 0,
                    type: 'image',
        			group: undefined,
                    time_stamp: ''
				},
                time_stamp: [],
			}
		},
		mounted() {
			this.handleQuery()
		},
		watch: {
        	"search.paging": function() {
				this.handleQuery()
			},
		},
		methods: {
			//获取列表
			async getTableList() {
				this.loading = true
				var res = await this.$API.system.resource.list.get(this.search)
				if(res.code == 200) {
					this.tableList = res.data.rows
					this.search.total = res.data.total
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				} 
                this.loading = false
			},
            handleType(e) {
                this.search.type = e
                this.search.page = 1
                this.getTableList()
            },
            handleChechBox(row) {
                return row.deletable == 1 ? true : false    
            },
			//搜索
			handleQuery() {
				this.getTableList()
			},
			//重置搜索
			resetQuery() {
				this.search = {
        			type: 'image',
        			group: undefined,
                    time_stamp: ''
				}
                this.time_stamp = []
				this.handleQuery()
			},
			//编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row, this.search)
				})
			},
			//查看
			table_show(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row, this.search)
				})
			},
			//批量删除
			batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？如果删除成功将不可恢复`, '提示', {
					type: 'warning'
				}).then(async () => {
					const loading = this.$loading()
                    let ids = []
                    for (let i = 0; i < this.selection.length; i++) {
                        ids.push(this.selection[i].id)
                    }
                    var res = await this.$API.system.resource.delete.delete(ids)
                    if(res.code == 200) {
                        this.$message.success("删除成功")
                        this.handleQuery()
                    } else {
                        this.$message.success(res.msg)
                    }
					loading.close()
				}).catch(() => {
                    this.$message.success("删除失败")
				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
			//本地更新数据
			handleSaveSuccess(mode, search){
				if(mode != 'show') {
					this.search = search
					this.handleQuery()
				}
			},
            //时间搜素
            changeTimeStamp(e) {
                this.search.time_stamp = e.toString()
            }
		}
	}
</script>

<style>
  .el-row {
    margin-bottom: 20px;
    &:last-child { margin-bottom: 0;}
  }
  .el-col {border-radius: 4px;}
  .bg-purple-dark {background: #99a9bf;}
  .bg-purple {background: #d3dce6;}
  .bg-purple-light {background: #e5e9f2;}
  .grid-content {border-radius: 4px;min-height: 36px;}
  .row-bg {padding: 10px 0;background-color: #f9fafc;}
</style>
